package com.commerceerp.system.controller;

import com.commerceerp.system.service.TokenService;
import com.commerceerp.system.util.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2018/6/10/010.
 */
@Api(description = "wish重定向token", value = "wish重定向token")
@Controller
@RequestMapping("")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    //获取wish授权码
    @ApiOperation(value = "获取wish授权码(官方回调)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "code", value = "code", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/wish/authorization_code")
    public String saveWishToken(String code, ModelMap modelMap){
        ApiResult result = tokenService.saveToken(code);
        modelMap.addAttribute("code",result.getCode());
        modelMap.addAttribute("msg",result.getMsg());
        return "success";
    }


    @ApiOperation(value = "获取wish发送授权码的链接")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "clientId", value = "clientId", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/wish/sendCode", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult sendCode(String clientId){

        return new ApiResult("https://merchant.wish.com/oauth/authorize?client_id="+clientId);
    }

    @ApiOperation(value = "验证门店是否授权")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "clientId", value = "clientId", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/wish/vailCode", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult vailCode(String clientId){

        return tokenService.vailCode(clientId);
    }

}
